package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.ServerInfo;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.UtilList;

import java.util.List;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2018/3/15 14:20
 * Changed by :  Benjamin.
 * Changed time: 2018/3/15 14:20
 * Class description:
 */

public class ServerListAdapter extends RecyclerView.Adapter<ServerListAdapter.ViewHolder> {
    public ClickListenter clickListenter;
    private List<ServerInfo> serverInfos;
    private Context context;
    private boolean openTab = false;//true关闭 false 打开抽屉

    public ServerListAdapter(Context context) {
        this.context = context;
    }

    public void setClickListenter(ClickListenter clickListenter) {
        this.clickListenter = clickListenter;
    }


    public void refresh(List<ServerInfo> serverInfos, boolean openTab) {
        this.serverInfos = serverInfos;
        this.openTab = openTab;
        this.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_server_info, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ServerInfo info = serverInfos.get(position);
        holder.infoName.setText(StringUtils.value(info.getServerName()));
        holder.infoIp.setText(StringUtils.value("IP地址:  " + info.getAddress()));
        holder.infoPort.setText(StringUtils.value("端口:  " + info.getPort()));
        holder.infoType.setText(StringUtils.value("类型:  " + info.getType()));
     /*   //判断是否是当前选中模块
        if ("1".equals(StringUtils.value(info.getCurrent()))) {
            holder.infoOpt.setVisibility(View.VISIBLE);
            holder.infoOpt.setImageResource(R.mipmap.choosen);
            holder.left.setBackgroundColor(context.getResources().getColor(R.color.color_select_bg));
        } else {
            holder.infoOpt.setVisibility(View.INVISIBLE);
            holder.left.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
        }
        if (!"1".equals(info.getEditable())) {
            if (!openTab) {
                holder.infoDelete.setVisibility(View.GONE);
                holder.infoEdit.setVisibility(View.GONE);
            } else {
                holder.infoOpt.setVisibility(View.GONE);
                holder.infoDelete.setVisibility(View.VISIBLE);
                holder.infoEdit.setVisibility(View.VISIBLE);
            }
        }*/

        if (openTab) {
            holder.infoOpt.setVisibility(View.GONE);
            if ("1".equals(info.getEditable())) {
                holder.infoDelete.setVisibility(View.GONE);
                holder.infoEdit.setVisibility(View.GONE);
            } else {
                holder.infoDelete.setVisibility(View.VISIBLE);
                holder.infoEdit.setVisibility(View.VISIBLE);
            }
        } else {
            holder.infoDelete.setVisibility(View.GONE);
            holder.infoEdit.setVisibility(View.GONE);
            if ("1".equals(info.getCurrent())) {
                holder.infoOpt.setVisibility(View.VISIBLE);
                holder.infoOpt.setImageResource(R.mipmap.choosen);
                holder.left.setBackgroundColor(context.getResources().getColor(R.color.color_select_bg));
            } else {
                holder.infoOpt.setVisibility(View.GONE);
                holder.left.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
            }
        }
        holder.left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListenter.onSelected(info);
            }
        });
        holder.infoEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListenter.onEdit(info);
            }
        });
        holder.infoDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListenter.onDelete(info);
            }
        });
    }

    @Override
    public int getItemCount() {
        return UtilList.getCount(serverInfos);
    }

    public interface ClickListenter {
        void onSelected(ServerInfo info);

        void onEdit(ServerInfo info);

        void onDelete(ServerInfo info);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView infoName;
        TextView infoIp;
        TextView infoPort;
        TextView infoType;
        ImageView infoOpt;
        ImageView infoEdit;
        ImageView infoDelete;
        LinearLayout left;

        public ViewHolder(View itemView) {
            super(itemView);
            this.infoName = itemView.findViewById(R.id.info_name);
            this.infoIp = itemView.findViewById(R.id.info_ip);
            this.infoPort = itemView.findViewById(R.id.info_port);
            this.infoType = itemView.findViewById(R.id.info_type);
            this.infoOpt = itemView.findViewById(R.id.info_opt);
            this.infoEdit = itemView.findViewById(R.id.info_edit);
            this.infoDelete = itemView.findViewById(R.id.info_delete);
            this.left = itemView.findViewById(R.id.info_left);
        }
    }
}
